import Vue from "vue"
import Vuex from "vuex"

Vue.use(Vuex)

// 打开系统就提取缓存数据,放到vuex实现持久化存储---------------------
let storPlayList = []
try {
	const value = uni.getStorageSync("playList")
	if(value){
		storPlayList = JSON.parse(value)
	}
} catch(e){}

const store = new Vuex.Store({
	state:{
		userInfo:{ 					//登录接口用户数据
			hasLogin:false
		},
		leaveTime:0,				//记录app隐藏的开始时间,作用:判断是否重新显示广告页
		playList:storPlayList,		//播放列表
	},
	mutations:{
		// 保存登录用户信息
		storeLogin(state,payLoad){
			const temp = {
				hasLogin:true,    //是否登录
				token:payLoad.token,
				userName:payLoad.userName,   //用户名称
			}
			state.userInfo = temp
		},
		// 退出登录清除用户信息
		storeLogout(state){
			const temp = {
				hasLogin:false,
				token:'',
				userName:'',   //用户名称
			}
			state.userInfo = temp
			uni.removeStorageSync("userInfo") // 清除浏览器缓存storage
		},
		//记录app隐藏的开始时间,判断是否重新显示广告页
		storeLeaveTime(state){
			let curTime = (new Date()).getTime()
			state.leaveTime = curTime
		},
		// 保存播放列表
		storePlayList(state,payload){
			state.playList = payload
			// 持久化存储
			uni.setStorageSync("playList",JSON.stringify(payload))
		}
	},
	actions:{
		
	}
})

export default store